<?php
declare(strict_types=1);

namespace LowCode\Traits\Vue;

use Illuminate\Support\Collection;

trait HasScript
{
    public function addScriptAfterImport(string $script): static
    {
        return $this;
    }


    protected function getScriptHtml(): string
    {
        $refHtml = $this->getRefHtml();
        $reactiveHtml = $this->getReactiveHtml();
        $watchHtml = $this->getWatchHtml();
        $computedHtml = $this->getComputedHtml();
        $methodHtml = $this->getMethodHtml();
        $lifeCycleHtml = $this->getLifeCycleHtml();

        $setup = "const setup = function(){
            return ()=>{}
        };";
        $defineHtml = $this->getDefinedHtml();
//        $defineProps =
        if ($this->type === "sfc"){
            $importHtml = $this->getImportHtml();
            $sfcContents = Collection::make([$importHtml, $defineHtml,$refHtml,$reactiveHtml,$watchHtml,$computedHtml,$methodHtml,$lifeCycleHtml,$setup]);
            $values = $sfcContents->reject(fn ($item)=>empty($item))->all();
            $sfcContent = $this->implode($values);

            return <<<VUE
<script setup>
{$sfcContent}
</script>
VUE;
        }else{
            $this->addVueImport("defineComponent");
            $importHtml = $this->getImportHtml();
            $sfcContents = Collection::make([$refHtml,$reactiveHtml,$watchHtml,$computedHtml,$methodHtml,$lifeCycleHtml,$setup]);
            $values = $sfcContents->reject(fn ($item)=>empty($item))->all();
            $sfcContent = $this->implode($values);
            $returnVariable = "return {".implode(",",$this->variables->all())."}";
            return <<<VUE
<script>
{$importHtml}
export default defineComponent({
    {$defineHtml}
    setup(props, { emit }){
        {$sfcContent}
        {$returnVariable}
    }
})
</script>
VUE;
        }
    }
}
